
#include "ContainerComponent.h"
#include "../SimpleDockApplication.h"
#include "MainComponent.h"
#include <vector>

//==============================================================================
ContainerComponent::ContainerComponent (ElementComponent * parent_, DockElement * root_) : root (root_),
																					       parentElement (parent_),
																						   isShown(false)
															   
{
	std::vector<DockElement*>::iterator it;
	
	for ( it = root->getChildren().begin() ; it < root->getChildren().end(); it++ )
	{
		ElementComponent * toAdd = new ElementComponent(this, *it);
		elements.add(toAdd);	
		addAndMakeVisible(toAdd);
	}
	setSize(1, 1);


}

ContainerComponent::~ContainerComponent()
{
}

int ContainerComponent::getNumElements()
{
	return elements.size();
}

ElementComponent * ContainerComponent::getElementAt(const int& index)
{
	if(index >= 0 && index < elements.size())
		return elements.getUnchecked(index);

	return 0;
}



void ContainerComponent::setContainerShown(const bool& shouldBeShown)
{
	isShown = shouldBeShown;
}

bool ContainerComponent::isContainerShown()
{
	return isShown;
}

//==============================================================================
void ContainerComponent::paint (Graphics& g)
{
	GradientBrush gb(Colour(0xaaaa66ff), 0, 52, Colour(0x00111111), 0, 20, false);
	g.setBrush(&gb);
	//g.setColour(Colours::grey.withAlpha(.3f));
	g.fillRoundedRectangle(0, 0, (float) getWidth(), 51, 10);
	//g.setColour(Colours::white);
	g.drawRoundedRectangle(0, 0, (float) getWidth(), 51, 10, 1);
}

void ContainerComponent::resized()
{
	int lastX = 10;
	int maxH = 0; 
	for(int i = 0; i < elements.size(); i++)
	{
		Component * c = elements.getUnchecked(i);
		c->setTopLeftPosition(lastX, 5);
		lastX += c->getWidth();
		if(c->getHeight() > maxH)
			maxH = c->getHeight();
	}

	setSize(lastX + 10, maxH + 10);

	if(getParentComponent()->isValidComponent())
		getParentComponent()->resized();
}

void ContainerComponent::addElement()
{	
}

void ContainerComponent::removeElement(ElementComponent * elementToRemove)
{
	elementToRemove->fadeOutComponent(300, 0, 20, 5);
	elements.removeObject(elementToRemove);
	resized();
}

void ContainerComponent::moveElement(ElementComponent * elem, ElementComponent * target)
{
	int from = elements.indexOf(elem);
	int to = elements.indexOf(target);

	elements.move(from, to);
	resized();
}

void ContainerComponent::setParentElement(ElementComponent * element)
{
	parentElement = element;
}

ElementComponent * ContainerComponent::getParentElement()
{
	return parentElement;
}

void ContainerComponent::mouseDown(const MouseEvent& e)
{
	(dynamic_cast<SimpleDockApplication*>(JUCEApplication::getInstance()))->getGUIActionsManager()->mouseDownOnApp(e);
}

void ContainerComponent::mouseUp(const MouseEvent& e)
{
	(dynamic_cast<SimpleDockApplication*>(JUCEApplication::getInstance()))->getGUIActionsManager()->mouseUp(this, e);
}

void ContainerComponent::mouseMove(const MouseEvent& e)
{
	(dynamic_cast<SimpleDockApplication*>(JUCEApplication::getInstance()))->getGUIActionsManager()->mouseMoveOnApp(e);

	int gamma = 2;
	int def = 32;

	for(int i = 0; i < elements.size(); i++)
	{
		ElementComponent * elem = elements.getUnchecked(i);
		
		int delta = abs(elem->getX() + 13 - e.x);
		float magnify = 1.0f + 64.0f / (float) (delta + 16); 
		elem->setSize(def * magnify, def * magnify);
	
		resized();
	}
}

void ContainerComponent::mouseEnter(const MouseEvent& e)
{
	(dynamic_cast<SimpleDockApplication*>(JUCEApplication::getInstance()))->getGUIActionsManager()->mouseEnterApp(e);
}

void ContainerComponent::mouseExit(const MouseEvent& e)
{
	(dynamic_cast<SimpleDockApplication*>(JUCEApplication::getInstance()))->getGUIActionsManager()->mouseExitApp(e);
}

